import axios from "axios";
import React, { useEffect, useMemo, useState } from "react";
function useCinemaList(){
    const [cinemaList,setcinemaList] =useState([])
    //电影院信息
    useEffect(()=>{
        //请求数据
        axios({
            method:'GET',
            url:"https://m.maizuo.com/gateway?cityId=440300&ticketFlag=1&k=2005318",
            headers:{
                "X-Client-Info": '{"a":"3000","ch":"1002","v":"5.2.0","e":"1654433035923688551579649"}',
                "X-Host": "mall.film-ticket.cinema.list"
            }    
        }).then(res=>{
        // console.log(res.data.data.cinemas)
        setcinemaList(res.data.data.cinemas)
        })
    },[])
    return{
        cinemaList
    }
}
function useFilter(cinemaList,mytext){

    const getCinemaList=useMemo(()=>cinemaList.filter(item=>{
        //统一转大写
        return item.name.toUpperCase().includes(mytext.toUpperCase())||item.address.toUpperCase().includes(mytext.toUpperCase())
    }),[cinemaList,mytext])
    return{
        getCinemaList
    }
}
function MyCinema(){
    
    const [mytext,setmytext] =useState("")
    const {cinemaList}=useCinemaList()
    const {getCinemaList}=useFilter(cinemaList,mytext)
    return(
        <div>
            <input value={mytext} onChange={(e)=>{
               setmytext(e.target.value)
            }}></input>
            {
                getCinemaList.map(item=>
                <dl key={item.cinemaId}>
                    <div>{item.name}</div>
                    <div>{item.address}</div>
                </dl>
                )
            }
        </div>
    )
}

export default MyCinema